package jjn.round1;

import commons.ListNode;

import java.util.HashSet;
import java.util.Set;

/**
 * @author Jjn
 * @since 2022/10/12 00:06
 */
public class LeetCode817_LinkedListComponents {

    public int numComponents(ListNode head, int[] nums) {
        Set<Integer> set = new HashSet<>();
        for (int num : nums) {
            set.add(num);
        }
        int total = 0;
        boolean last = false;
        while (head != null) {
            int val = head.val;
            if (set.contains(val)) {
                last = true;
                head = head.next;
                continue;
            }
            if (last && !set.contains(val)) {
                total++;
                last = false;
            }
            head = head.next;
        }

        if (last) {
            total++;
        }
        return total;
    }

}
